package name.sakanacatcher.service;

import name.sakanacatcher.entity.Position;
import name.sakanacatcher.entity.User;
import name.sakanacatcher.repository.PositionRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.transaction.Transactional;
import java.util.List;
import java.util.Map;

@Service
public class PositionService {
    @Autowired
    PositionRepository positionRepository;

    @Autowired
    UserService userService;

    @Autowired
    ComService comService;

    public PositionRepository getPositionRepository() {
        return positionRepository;
    }

    public Position parseMapToPosition(Map<String,Object> data){
        List<String> typeList = (List<String>) data.get("type");
        Object[] labels = typeList.toArray();
        String label = StringUtils.arrayToDelimitedString(labels,"-");
        System.out.println(data.get("cid"));
        Position position = new Position(
                (String) data.get("name"),
                (int) data.get("wageMin"),
                (int) data.get("wageMax"),
                userService.getUserRepository().getById((int)data.get("hr")),
                comService.getComRepository().getById((int) data.get("cid")),
                (String) data.get("address"),
                label,
                (String) data.get("description")
        );
        if(data.containsKey("id") && positionRepository.existsById((int) data.get("id"))){
            position.setId((int) data.get("id"));
        }
        return position;
    }
    @Transactional
    public boolean updatePosition(Map<String,Object> data){
        Position position = parseMapToPosition(data);
        Position newPosition = positionRepository.save(position);
        return positionRepository.existsById(newPosition.getId());
    }

    @Transactional
    public boolean deletePosition(Map<String,Object> data){
        int id = (int) data.get("id");
        if(positionRepository.existsById(id)){
            Position position = positionRepository.getById(id);
            position.setCompany(null);
            position.setHr(null);
            Position newPosition = positionRepository.save(position);
            positionRepository.delete(newPosition);
            return !positionRepository.existsById(id);
        }
        return false;
    }

    public boolean existApplicantByUsername(String username, int pid) {
        Position position = getPositionRepository().getById(pid);
        for(User user:position.getApplicants()){
            if(user.getUsername().equals(username)){
                return true;
            }
        }
        return false;
    }
    @Transactional
    public void addApplicant(String username, int pid) {
        Position position = getPositionRepository().getById(pid);
        position.getApplicants().add(userService.getUserRepository().getByUsername(username));
        positionRepository.save(position);
    }
}
